﻿using System;
using System.Collections.Specialized;

namespace UnitTests.Logging
{
  public class ObservableCollectionLogger : LoggableObject, ITracker
  {
    private readonly INotifyCollectionChanged _src;

    public ObservableCollectionLogger(INotifyCollectionChanged src)
    {
      _src = src;
      _src.CollectionChanged += SrcOnCollectionChanged;
    }

    private void SrcOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
      switch (e.Action)
      {
        case NotifyCollectionChangedAction.Add:
          var addArg = (LoggableObject)e.NewItems[0];
          AppendLog(new LogMessage("{0}.CollectionChanged(" + e.Action + ": {1})", this, addArg));
          break;
        case NotifyCollectionChangedAction.Remove:
          var removeArg = (LoggableObject)e.OldItems[0];
          AppendLog(new LogMessage("{0}.CollectionChanged(" + e.Action + ": {1})", this, removeArg));
          break;
        case NotifyCollectionChangedAction.Reset:
          AppendLog(new LogMessage("{0}.CollectionChanged(Reset)", this));
          break;
        default:
          throw new NotImplementedException();
      }
    }

    public void Dispose()
    {
      _src.CollectionChanged += SrcOnCollectionChanged;
    }
  }
}